package com.irdstudio.bsp.executor.core.dao;

import com.irdstudio.bsp.executor.core.dao.domain.MigrateGovernTask;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移治理任务类-[表名: 迁移治理任务(migrate_govern_task)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateGovernTaskDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateGovernTaskDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateGovernTask(MigrateGovernTask migrateGovernTask) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_govern_task ( "
                + "govern_task_id,govern_task_src,govern_src_id,govern_task_desc,impact_analysis,migrate_comment,dev_comment,test_comment,tech_comment,busi_comment,source_incharge,target_incharge,govern_task_state,task_begin_date,task_require_finish_date,task_real_finish_date"
                + ") values ("+ "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateGovernTask.getGovernTaskId());
            ps.setObject(2, migrateGovernTask.getGovernTaskSrc());
            ps.setObject(3, migrateGovernTask.getGovernSrcId());
            ps.setObject(4, migrateGovernTask.getGovernTaskDesc());
            ps.setObject(5, migrateGovernTask.getImpactAnalysis());
            ps.setObject(6, migrateGovernTask.getMigrateComment());
            ps.setObject(7, migrateGovernTask.getDevComment());
            ps.setObject(8, migrateGovernTask.getTestComment());
            ps.setObject(9, migrateGovernTask.getTechComment());
            ps.setObject(10, migrateGovernTask.getBusiComment());
            ps.setObject(11, migrateGovernTask.getSourceIncharge());
            ps.setObject(12, migrateGovernTask.getTargetIncharge());
            ps.setObject(13, migrateGovernTask.getGovernTaskState());
            ps.setObject(14, migrateGovernTask.getTaskBeginDate());
            ps.setObject(15, migrateGovernTask.getTaskRequireFinishDate());
            ps.setObject(16, migrateGovernTask.getTaskRealFinishDate());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateGovernTask is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateGovernTask migrateGovernTask) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_govern_task where 1=1"
                + " AND govern_task_id = ? "
                );
            ps.setObject(1, migrateGovernTask.getGovernTaskId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateGovernTask is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateGovernTask migrateGovernTask) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_govern_task set "
                + " govern_task_id = ? ,"
                + " govern_task_src = ? ,"
                + " govern_src_id = ? ,"
                + " govern_task_desc = ? ,"
                + " impact_analysis = ? ,"
                + " migrate_comment = ? ,"
                + " dev_comment = ? ,"
                + " test_comment = ? ,"
                + " tech_comment = ? ,"
                + " busi_comment = ? ,"
                + " source_incharge = ? ,"
                + " target_incharge = ? ,"
                + " govern_task_state = ? ,"
                + " task_begin_date = ? ,"
                + " task_require_finish_date = ? ,"
                + " task_real_finish_date = ? "
                + " where 1=1"
                + " AND govern_task_id = ? "
                );
            ps.setObject(1, migrateGovernTask.getGovernTaskId());
            ps.setObject(2, migrateGovernTask.getGovernTaskSrc());
            ps.setObject(3, migrateGovernTask.getGovernSrcId());
            ps.setObject(4, migrateGovernTask.getGovernTaskDesc());
            ps.setObject(5, migrateGovernTask.getImpactAnalysis());
            ps.setObject(6, migrateGovernTask.getMigrateComment());
            ps.setObject(7, migrateGovernTask.getDevComment());
            ps.setObject(8, migrateGovernTask.getTestComment());
            ps.setObject(9, migrateGovernTask.getTechComment());
            ps.setObject(10, migrateGovernTask.getBusiComment());
            ps.setObject(11, migrateGovernTask.getSourceIncharge());
            ps.setObject(12, migrateGovernTask.getTargetIncharge());
            ps.setObject(13, migrateGovernTask.getGovernTaskState());
            ps.setObject(14, migrateGovernTask.getTaskBeginDate());
            ps.setObject(15, migrateGovernTask.getTaskRequireFinishDate());
            ps.setObject(16, migrateGovernTask.getTaskRealFinishDate());
            ps.setObject(17, migrateGovernTask.getGovernTaskId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateGovernTask is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateGovernTask queryByPk(MigrateGovernTask migrateGovernTask) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateGovernTask result = null;
        try {
            ps = conn.prepareStatement("select govern_task_id,govern_task_src,govern_src_id,govern_task_desc,impact_analysis,migrate_comment,dev_comment,test_comment,tech_comment,busi_comment,source_incharge,target_incharge,govern_task_state,task_begin_date,task_require_finish_date,task_real_finish_datefrom migrate_govern_task where 1=1 "
                + " AND govern_task_id = ? "
                );
            ps.setObject(1, migrateGovernTask.getGovernTaskId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateGovernTask();
				result.setGovernTaskId(rs.getString("govern_task_id"));
				result.setGovernTaskSrc(rs.getString("govern_task_src"));
				result.setGovernSrcId(rs.getString("govern_src_id"));
				result.setGovernTaskDesc(rs.getString("govern_task_desc"));
				result.setImpactAnalysis(rs.getString("impact_analysis"));
				result.setMigrateComment(rs.getString("migrate_comment"));
				result.setDevComment(rs.getString("dev_comment"));
				result.setTestComment(rs.getString("test_comment"));
				result.setTechComment(rs.getString("tech_comment"));
				result.setBusiComment(rs.getString("busi_comment"));
				result.setSourceIncharge(rs.getString("source_incharge"));
				result.setTargetIncharge(rs.getString("target_incharge"));
				result.setGovernTaskState(rs.getString("govern_task_state"));
				result.setTaskBeginDate(rs.getString("task_begin_date"));
				result.setTaskRequireFinishDate(rs.getString("task_require_finish_date"));
				result.setTaskRealFinishDate(rs.getString("task_real_finish_date"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateGovernTask is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateGovernTask 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateGovernTask> queryAll(MigrateGovernTask migrateGovernTask) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateGovernTask> result = new ArrayList<>();
        try {
            String sql = "select govern_task_id,govern_task_src,govern_src_id,govern_task_desc,impact_analysis,migrate_comment,dev_comment,test_comment,tech_comment,busi_comment,source_incharge,target_incharge,govern_task_state,task_begin_date,task_require_finish_date,task_real_finish_datefrom migrate_govern_task where 1=1 ";
                if (migrateGovernTask.getGovernTaskId() != null) {
                    sql = sql + " AND govern_task_id =  '" + migrateGovernTask.getGovernTaskId() + "'";
                }
                if (migrateGovernTask.getGovernTaskSrc() != null) {
                    sql = sql + " AND govern_task_src =  '" + migrateGovernTask.getGovernTaskSrc() + "'";
                }
                if (migrateGovernTask.getGovernSrcId() != null) {
                    sql = sql + " AND govern_src_id =  '" + migrateGovernTask.getGovernSrcId() + "'";
                }
                if (migrateGovernTask.getGovernTaskDesc() != null) {
                    sql = sql + " AND govern_task_desc =  '" + migrateGovernTask.getGovernTaskDesc() + "'";
                }
                if (migrateGovernTask.getImpactAnalysis() != null) {
                    sql = sql + " AND impact_analysis =  '" + migrateGovernTask.getImpactAnalysis() + "'";
                }
                if (migrateGovernTask.getMigrateComment() != null) {
                    sql = sql + " AND migrate_comment =  '" + migrateGovernTask.getMigrateComment() + "'";
                }
                if (migrateGovernTask.getDevComment() != null) {
                    sql = sql + " AND dev_comment =  '" + migrateGovernTask.getDevComment() + "'";
                }
                if (migrateGovernTask.getTestComment() != null) {
                    sql = sql + " AND test_comment =  '" + migrateGovernTask.getTestComment() + "'";
                }
                if (migrateGovernTask.getTechComment() != null) {
                    sql = sql + " AND tech_comment =  '" + migrateGovernTask.getTechComment() + "'";
                }
                if (migrateGovernTask.getBusiComment() != null) {
                    sql = sql + " AND busi_comment =  '" + migrateGovernTask.getBusiComment() + "'";
                }
                if (migrateGovernTask.getSourceIncharge() != null) {
                    sql = sql + " AND source_incharge =  '" + migrateGovernTask.getSourceIncharge() + "'";
                }
                if (migrateGovernTask.getTargetIncharge() != null) {
                    sql = sql + " AND target_incharge =  '" + migrateGovernTask.getTargetIncharge() + "'";
                }
                if (migrateGovernTask.getGovernTaskState() != null) {
                    sql = sql + " AND govern_task_state =  '" + migrateGovernTask.getGovernTaskState() + "'";
                }
                if (migrateGovernTask.getTaskBeginDate() != null) {
                    sql = sql + " AND task_begin_date =  '" + migrateGovernTask.getTaskBeginDate() + "'";
                }
                if (migrateGovernTask.getTaskRequireFinishDate() != null) {
                    sql = sql + " AND task_require_finish_date =  '" + migrateGovernTask.getTaskRequireFinishDate() + "'";
                }
                if (migrateGovernTask.getTaskRealFinishDate() != null) {
                    sql = sql + " AND task_real_finish_date =  '" + migrateGovernTask.getTaskRealFinishDate() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateGovernTask obj = new MigrateGovernTask();
				obj.setGovernTaskId(rs.getString("govern_task_id"));
				obj.setGovernTaskSrc(rs.getString("govern_task_src"));
				obj.setGovernSrcId(rs.getString("govern_src_id"));
				obj.setGovernTaskDesc(rs.getString("govern_task_desc"));
				obj.setImpactAnalysis(rs.getString("impact_analysis"));
				obj.setMigrateComment(rs.getString("migrate_comment"));
				obj.setDevComment(rs.getString("dev_comment"));
				obj.setTestComment(rs.getString("test_comment"));
				obj.setTechComment(rs.getString("tech_comment"));
				obj.setBusiComment(rs.getString("busi_comment"));
				obj.setSourceIncharge(rs.getString("source_incharge"));
				obj.setTargetIncharge(rs.getString("target_incharge"));
				obj.setGovernTaskState(rs.getString("govern_task_state"));
				obj.setTaskBeginDate(rs.getString("task_begin_date"));
				obj.setTaskRequireFinishDate(rs.getString("task_require_finish_date"));
				obj.setTaskRealFinishDate(rs.getString("task_real_finish_date"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateGovernTask is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
